package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-11
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduTeacherService teacherService;

    //此注解为将首页前4条热门名师加入redis缓存
    @Cacheable(value = "banner",key = "'selectHotTeacher'")
    @Override
    //查询前4条热门名师
    public List<EduTeacher> selectHotTeacher() {
        QueryWrapper<EduTeacher> wrapperTeacher=new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        List<EduTeacher> teacherList=teacherService.list(wrapperTeacher);
        return teacherList;
    }


    //分页查询讲师的方法(底层实现)
    @Override
    public Map<String, Object> getTeacherFronList(Page<EduTeacher> pageParam) {
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        //降序排列
        wrapper.orderByDesc("id");
        //把分页数据封装到pageParam对象中来
        baseMapper.selectPage(pageParam,wrapper);


        List<EduTeacher> records = pageParam.getRecords();  //每页数据list集合
        long current = pageParam.getCurrent();              //当前页
        long pages = pageParam.getPages();                  //总页数
        long size = pageParam.getSize();                    //每页记录数
        long total = pageParam.getTotal();                  //总记录数
        boolean hasNext = pageParam.hasNext();              //是否有下一页
        boolean hasPrevious = pageParam.hasPrevious();      //是否有上一页

        //把分页数据获取出来，放到map集合
        Map<String,Object> map=new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
